<?php

namespace App\Admin\Controllers\SysAccount;

use App\Models\Common\Media;
use App\Models\Open\SysAccountQrcodeReply;
use App\Models\Open\WechatKfMedia;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class SysAccountQrcodeReplyController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(SysAccountQrcodeReply::with(['media']), function (Grid $grid) {
            $grid->enableDialogCreate();
            $grid->disableEditButton();
            $grid->showQuickEditButton('编辑');

            $grid->column('scene');
            $grid->column('media.title');
            $grid->column('media.typeid')->using(trans('wechat-kf-media.options.types'));
            $grid->column('media.content');
            $grid->column('media.url');
            $grid->column('media.pic_id')->display(function ($pic_id) {
                return $pic_id ? Media::where('id', $pic_id)->pluck('url')->toArray() : [];
            })->image('', 50);
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SysAccountQrcodeReply(), function (Show $show) {
            $show->field('id');
            $show->field('scene');
            $show->field('kf_media_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(SysAccountQrcodeReply::with(['media']), function (Form $form) {
            $form->text('scene');
            $form->selectTable('kf_media_id')
                ->title('Kf Media')
                ->from(KfMediaTable::make())
                ->model(WechatKfMedia::class, 'id', 'title');
        });
    }
}
